<?php if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class star_controller extends PZ_Controller{

	public function __construct() {
		parent::__construct();
		$this->authfunctions = array('cropcover','getstaruser','delete','update');
		//$this->seo_title('达人秀');
	}
	public function beforeAction(){
		$result = parent::beforeAction();
		if($result){
			if(!$this->settings['star_open']){
				$this->message(lang('star_not_open','tip'), lang('star_not_open_tip','tip'));
				$result = false;
			}
		}
		return $result;
	}
	public function show(){
		$this->starcateogries = load_db("star/starcategory")->getAllCategories();
		$this->prepareShow();
		$this->load_seo('star');
		$this->output("star/index");
	}

	private function prepareShow(){
		$stardb = load_db("star");
		$staruser_cates = $stardb->get_staruser_cache();
		foreach ($staruser_cates as $key=>$staruser_cate){
			$starusers = $staruser_cate['staruser'];
			$staruser_cates[$key]['staruser'] = $this->fillView($starusers);
		}
		$this->startuser_cates = $staruser_cates;
	}

	private function fillView($starusers){
		$user_id = $this->islogin?$this->cuser['uid']:0;
		foreach ($starusers as $key=>$star){
			$star['star_cover'] = $this->prepareCover(str_to_arr($star['star_cover']));
			$style = $star['star_cover']['style']?$star['star_cover']['style']:'star_1';
			$this->staruser = $star;
			$starusers[$key]['star_view'] = $this->render('star/'.$style);
			$starusers[$key]['relation_view'] = $this->relationView($user_id,$star['user_id']);
		}
		return $starusers;
	}

	private function prepareCover($cover){
		for ($i=1;$i<4;$i++){
			if($cover['s'.$i.'_image_path']){
				$cover['s'.$i.'_image_path'] = thumburl($cover['s'.$i.'_image_path'],false,$cover['s'.$i.'_cloud']);
			}
		}
		return $cover;
	}

	public function create(){
		if(!$this->isadmin&&!$this->iseditor){
			$this->ajax_failed_response(lang('illegal_operation','tip'));
		}

		$star_reason = $this->param("sreason");
		$uid = $this->param("uid");
		$rid = $this->param("rid");
		if(!$uid||!$star_reason){
			$this->ajax_failed_response(lang('illegal_operation','tip'));
			return;
		}

		$applydb = load_db('misc/apply');
		if($applydb->checkstar_exits($uid)){
			$this->ajax_failed_response(lang('already_staruser_failed','tip'));
			return;
		}else {
			$applyid = $applydb->add_starapply($uid,$rid,$star_reason);
			$applydb->agree($applyid);

			$this->ajax_success_response(null, '');
			return;
		}
		$this->ajax_failed_response(lang('500_operatefaild','tip'));
		return;
	}

	public function update(){
		$staruser = load_db('star')->find_one(array('user_id'=>$this->cuser['uid']));
		if($staruser){
			$star_reason = $this->param('star_reason');
			load_db('star')->update(array('user_id'=>$userid),array('star_reason'=>$star_reason));
			$this->ajax_success_response(null, lang('200_operatesuccess','tip'));
			return;
		}
		$this->ajax_failed_response(lang('500_operatefaild','tip'));
	}

	public function getstaruser(){
		$userid = $this->param('uid');
		$shareid = $this->param('sid');
		if(!$userid||!$shareid||(!$this->iseditor&&!$this->isadmin&&!$this->cuser['isstar'])){
			$this->ajax_failed_response(lang('illegal_operation','tip'));
			return;
		}
		$staruser = load_db('star')->find_one(array('user_id'=>$userid));
		if($staruser){
			$share = load_api('share')->getShareById($shareid);
			$user = load_api('user')->getUserByid($userid);
			$staruser['nickname'] = $user['nickname'];
			$staruser['bio'] = $user['bio'];
			$staruser['user_title'] = $user['user_title'];
			$staruser['total_like'] = $user['total_like'];
			$staruser['avatar'] = useravatar($userid, 'large');
			$staruser['star_cover'] = $this->prepareCover(str_to_arr($staruser['star_cover']));
			$staruser['home'] = site_url('home/user?uid='.$userid);
			$staruser['image_path'] = thumburl($share['image_path_0'],'middle',$share['cloud']);
			$this->ajax_success_response($staruser, '');
			return;
		}else{
			$user = load_api('user')->getUserByid($userid);
			$staruser['user_id'] = $userid;
			$staruser['home'] = site_url('home/user?uid='.$userid);
			$staruser['nickname'] = $user['nickname'];
			$staruser['bio'] = $user['bio'];
			$staruser['avatar'] = useravatar($userid, 'large');
			$this->ajax_failed_response('',$staruser);
			return;
		}
	}

	public function cropcover(){
		$userid = $this->param('uid');
		$categoryid = $this->param('cid');
		$shareid = $this->param('sid');
		if(!$shareid||!$categoryid||!$userid||(!$this->iseditor&&!$this->isadmin&&!$this->cuser['isstar'])){
			$this->ajax_failed_response(lang('illegal_operation','tip'));
			return;
		}

		$position = $this->param("position");
		$x = $this->param("x");
		$y = $this->param("y");
		$w = $this->param("w");
		$h = $this->param("h");
		$js_w = $this->param("js_w");
		$js_h = $this->param("js_h");
		$ww = $this->param("ww");
		$hh = $this->param("hh");
		$style = $this->param("sy",'star_1');

		$share = load_api('share')->getShareById($shareid);
		if($share){
			$imagelib = &load_class('ImageLib');
			$imagepath = thumburl($share['image_path_0'],'large',false,true);
			$starimagepath = thumburl($share['image_path_0'],'star',false,true);
			load_api('event')->cropimagestart($imagepath,$share['cloud']);
				
			$image_size = @getimagesize($imagepath);
			$weight=$image_size[0];
			$height=$image_size[1];
			if($js_w<$weight){
				$scale = $js_w/$weight;
			}elseif ($js_h<$height){
				$scale = $js_h/$height;
			}else{
				$scale = 1;
			}
			$x = $x/$scale;
			$y = $y/$scale;
			$w = $w/$scale;
			$h = $h/$scale;
			$imagelib->crop_image($imagepath,$starimagepath,$x,$y,$w,$h);
			$imagelib->create_thumb($starimagepath,NULL,$ww,$hh,$starimagepath);
			load_api('event')->cropimageend($imagepath,$starimagepath,$share['cloud']);
		}

		$ptx_staruser = load_db('star');
		$staruser = $ptx_staruser->find_one(array('user_id'=>$userid));
		if($staruser){
			$covers = str_to_arr($staruser['star_cover']);
			$covers['style'] = $style;
			$covers['s'.$position.'_sid'] = $shareid;
			$covers['s'.$position.'_cloud'] = $share['cloud'];
			$covers['s'.$position.'_image_path'] = thumbpath($share['image_path_0'],'star');
			$star_cover = array_to_str($covers);
			$ptx_staruser->update(array('user_id'=>$userid),array('star_cover'=>$star_cover));
			$ptx_staruser->update_staruser_cache();
			$data['img']= thumburl($share['image_path_0'],'star',$share['cloud']).'?'.uniqid();
			$this->ajax_success_response($data, lang('200_operatesuccess','tip'));
			return;
		}
		$this->ajax_failed_response(lang('500_operatefaild','tip'));
		return;
	}

}